<template>
  <div class="innovate-container-padding">
    <el-form
      :inline="true"
      :model="formInline"
      class="form-inline"
      label-width="auto"
    >
      <el-form-item label="上报单位">
        <el-select v-model="formInline.reportUnitId" placeholder="请选择">
          <el-option
            v-for="j in dptList"
            :key="j.id"
            :label="j.name"
            :value="j.id"
          />
        </el-select>

        <!-- date -->
      </el-form-item>
      <el-form-item label="上报日期:">
        <el-date-picker
          v-model="reportDate"
          type="daterange"
          range-separator="→"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          clearable
          @change="handTimeChange"
        />
      </el-form-item>
      <el-form-item label="关键字：">
        <el-input v-model="formInline.baseSituation" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          style="margin-right: 10px"
          @click="handSearch"
        >
          搜索
        </el-button>
        <el-button @click="handReset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-divider />
    <div class="buttonList">
      <el-button type="primary" @click="handAdd">
        自主创新产品认定上报
      </el-button>
      <el-button :loading="downLoading" @click="handExport">
        导出搜索结果
      </el-button>
    </div>
    <vxe-grid v-bind="gridOptions" :height="height">
      <template #operate="{ row }">
        <el-button type="primary" link @click="viewEvent(row, 1)">
          详情
        </el-button>
        <!-- <el-button type="primary" link @click="viewEvent(row, 2)">
          修改
        </el-button>
        <el-button type="danger" link @click="del(row)">删除</el-button> -->
      </template>
      <template #pager>
        <!--使用 pager 插槽-->
        <vxe-pager
          v-model:current-page="tablePage.currentPage"
          v-model:page-size="tablePage.pageSize"
          :layouts="[
            'Sizes',
            'PrevJump',
            'PrevPage',
            'Number',
            'NextPage',
            'NextJump',
            'FullJump',
            'Total',
          ]"
          :total="tablePage.total"
          @page-change="handlePageChange"
        />
      </template>
    </vxe-grid>
  </div>
</template>

<script setup lang="ts">
  import { ref, reactive, onBeforeMount, onBeforeUnmount, inject } from 'vue'
  import { gridOptions } from './table/column'
  import { useRouter } from 'vue-router'
  import { getSimpleDeptList } from '@/api/system/dept'
  import { VxePagerEvents } from 'vxe-table'
  import { ElMessageBox, ElMessage } from 'element-plus'
  import { getInnovateList, getInnovateExport } from '@/api/innovate'
  import { useDownload } from '@/utils/useDownload'

  const router = useRouter()
  const reportDate = ref('')
  // 搜索表单
  const formInline = ref({
    reportUnitId: '',
    reportDateStart: '',
    reportDateEnd: '',
    baseSituation: '',
  })
  // 搜索
  const handSearch = () => {
    getList()
  }

  const handReset = () => {
    formInline.value = {
      reportUnitId: '',
      reportDateStart: '',
      reportDateEnd: '',
      baseSituation: '',
    }
    getList()
  }

  // 导出搜索结果
  const { downLoading, downloadFile } = useDownload(
    getInnovateExport,
    '自助创新产品认定.xls'
  )
  const handExport = async () => {
    await downloadFile(formInline.value)
  }

  const handTimeChange = (value: Array<string>) => {
    formInline.value.reportDateStart = value[0]
    formInline.value.reportDateEnd = value[1]
  }
  const dptList = ref([])
  const getDptList = () => {
    getSimpleDeptList().then((res: any) => {
      dptList.value = res.data
    })
  }

  const getList = () => {
    const params = {
      pageNo: tablePage.currentPage,
      pageSize: tablePage.pageSize,
      ...formInline.value,
    }
    getInnovateList(params).then((res: any) => {
      tablePage.total = res.data.total
      gridOptions.data = res.data.list
    })
  }

  const handAdd = () => {
    router.push('/innovate/add')
  }

  const viewEvent = (row: any, type: number) => {
    if (type == 1) {
      router.push({ path: '/innovate/detail', query: { id: row.id } })
    }
  }

  const tablePage = reactive({
    total: 0,
    currentPage: 1,
    pageSize: 10,
  })

  const handlePageChange: VxePagerEvents.PageChange = ({
    currentPage,
    pageSize,
  }) => {
    tablePage.currentPage = currentPage
    tablePage.pageSize = pageSize
    getList()
  }

  onMounted(() => {
    getDptList()
    getList()
  })

  const del = (row: any) => {
    ElMessageBox.confirm('确定删除该条专利?', '删除专利', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
      .then(() => {
        ElMessage({
          type: 'success',
          message: 'Delete completed',
        })
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: 'Delete canceled',
        })
      })
  }
  const height = ref(0)
  interface BaseTableHeight {
    (padding: number): number
  }
  const $baseTableHeight = inject<BaseTableHeight>('$baseTableHeight')
  onBeforeMount(() => {
    window.addEventListener('resize', handleHeight)
  })

  onBeforeUnmount(() => {
    window.removeEventListener('resize', handleHeight)
  })

  const handleHeight = () => {
    height.value = $baseTableHeight(0)
  }
</script>

<style scoped lang="scss">
  .buttonList {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
  }

  :deep(.el-divider) {
    margin: 8px 0 20px 0 !important;
  }

  .form-inline {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-column-gap: 20px;

    :deep(.el-form-item) {
      margin-left: 0;
      margin-right: 0;
    }
  }

  .keyword {
    display: flex;
    align-items: center;
  }
</style>
